package org.example.myframework.frame.demo.designPattern.filterPattern;

import java.util.List;
import java.util.stream.Collectors;

public class ArticleFilterDemo {

    private List<ArticleFilterEnum> articleFilterEnumList;

    private List<IArticleFilter> iArticleFilterList;

    private void init() {
        // 初始化过滤器
        this.iArticleFilterList = ArticleFilterFactory.createArticleFilter(articleFilterEnumList);

    }

    public void doFilter(List<Article> articleList) {
        // 在经过过滤器过滤之后，拿到的将是被我们的过滤器过滤之后的文章List
        articleList.stream().filter(article -> {
            ArticleContext articleContext = new ArticleContext();
            articleContext.setArticle(article);
            return doFilter(articleContext);
        }).collect(Collectors.toList());
    }

    private boolean doFilter(ArticleContext articleContext) {
        for (IArticleFilter iArticleFilter : iArticleFilterList) {
            if (iArticleFilter.doFilter(articleContext)) {
                return false;
            }
        }
        return true;
    }

}
